#pragma once
#include<iostream>

using namespace std;
namespace gg_bond
{
	template<class T>
	class list_node
	{
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;

		list_node(const T& x = T())
			:_data(x)
			, _next(nullptr)
			, _prev(nullptr)
		{

		}
	};

	template<class T>
	struct __list_iterator
	{
		typedef list_node<T> Node;
		typedef __list_iterator<T> iterator;
		Node* _node;

		__list_iterator(Node* node)
			:_node(node)
		{

		}

		bool operator!=(const iterator& it) const
		{
			return _node != it._node;
		}

		bool operator==(const iterator& it) const
		{
			return _node == it._node;
		}

		T& operator*()
		{
			return _node->_data;
		}



		iterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		iterator& operator++(int)
		{
			iterator tmp(*this);
			_node = _node->_next;
			return *tmp;
		}

		iterator& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		iterator& operator--(int)
		{
			iterator tmp(*this);
			_node = _node->_prev;
			return *tmp;
		}

	};

	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef __list_iterator<T> iterator;
		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		void push_back(const T& x)
		{
			/*Node* tail = _head->prev;
			Node* newnode = new Node;

			tail->_next = newnode;
			newnode->_prev = tail;
			newnode->_next = _head;
			_head->prev = newnode;*/
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;

			Node* newnode = new Node(x);

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
		}
		
		void erase(iterator pos)
		{
			assert(pos != end());
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;

			return iterator(next);
		}

		~list()
		{

		}
	private:
		Node* _head;
	};


	void test()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(4);
		lt.push_back(21);
		lt.push_back(15);
		lt.push_back(66);

		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout<< *it << " ";
			++it;
		}
		cout << endl;
	}
}



